package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public R<Employee> login(Employee employee) {
        //1.根据用户名查找用户
        //判断用户是否为null,为null就不存在
        Employee loginEmployee = employeeMapper.login(employee);
        if (loginEmployee == null){
            throw new BusinessException("用户名或密码错误!");
        }

        //2.如果用户名存在判断密码
        //对用户名密码进行加密
        String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //与数据库密码进行对比
        if ( !loginEmployee.getPassword().equals(md5)){
            //不一样,密码错误
            throw new BusinessException("用户名或密码错误!");
        }

        //3.判断用户的状态是否为禁用
        if (loginEmployee.getStatus()==0){
            throw new BusinessException("您的账号以被冻结,请联系管理员解决!");
        }

        //4.如果上面都正确则登入成功
        return R.success(loginEmployee);
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @Override
    public boolean save(Employee employee) {
        //1.校验账号是否唯一
        //获取用户
        Employee dbemployee = employeeMapper.login(employee);
        //判断用户名是否为null,不为null说明用户名已经存在
        if (dbemployee != null){
            throw new BusinessException("用户名已经存在,请修改!");
        }

        //2.完善数据
        //默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //默认状态
        employee.setStatus(1);

        //创建时间
        employee.setCreateTime(LocalDateTime.now());

        //修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //调用mapper层添加到数据库
        employeeMapper.save(employee);
        return true;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //1.设置分页查询第几页和每页多少条
        PageHelper.startPage(page,pageSize);

        //2.执行查询所有数据的sql语句方法
        List<Employee> employeeList = employeeMapper.findByName(name);

        //3.使用pageInfo封装上面的结果
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        Page<Employee> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    /**
     * 修改业务方法
     *
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        //1.设置修改时间
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.update(employee)>0;
    }

    /**
     * 修改回显数据
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return R.success(employee);
    }
}
